package psi.springapp.mvc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import model.Party;
import psi.springapp.mvc.repositories.PartyRepository;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IDEA.
 * User: Ѱ
 * Date: 28.09.14
 * Time: 10:39
 */
@Service
public class PartyService {
    @Autowired
    private PartyRepository partyRepository;

    public PartyService() {
    }

    public Iterable<Party> allParty() {
        return partyRepository.findAll();
    }

    public List<Party> partyList() {
        List<Party> parties = new LinkedList<Party>();
        Iterator<Party> i = allParty().iterator();
        Party tmp;
        while (i.hasNext()) {
            tmp = i.next();
            parties.add(tmp);
        }
        return parties;
    }

    public void saveParty(Party party) {
        partyRepository.save(party);
    }

    public void deleteParty(Party party) {
        partyRepository.delete(party);
    }

    public void deleteAll() {
        partyRepository.deleteAll();
    }

    public Party getByName(String name) {
        Iterator<Party> iterator = allParty().iterator();
        Party tmp = null;
        while (iterator.hasNext()) {
            tmp = iterator.next();
            if (tmp.getName().equalsIgnoreCase(name))
                return tmp;
        }
        return tmp;
    }

    public Party getById(int id) {
        Iterator<Party> iterator = allParty().iterator();
        Party tmp = null;
        while (iterator.hasNext()) {
            tmp = iterator.next();
            if (tmp.getId_party() == id)
                return tmp;
        }
        return tmp;
    }
}
